﻿using System;
using System.Collections.Generic;
using System.Data;
using BLL;
using Utils;
namespace UI.ModTrain
{
    public partial class TrainAdd : System.Web.UI.Page
    {
        TrainBiz trainBiz = new TrainBiz();
        string trainID = "";
        string account = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                User user = Session["user"] as User;
                account = user.Account;
            }
            else
            {
                JSUtility.Alert(this, "您没有登录!请先登录。");
                return;
            }
            if (Request.Params["TrainID"] != null)
            {
                trainID = Request.Params["TrainID"].ToString();
                btnAdd.Enabled = false;
            }
            else
            {
                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;
            }
            if (!IsPostBack)
            {
                JSUtility.AddConfirm(btnDelete, "您真的要删除该员工的信息吗？");
                txtTrainTime.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
                if (trainID != "")
                {
                    btnAdd.Enabled = false;
                    btnDelete.Enabled = true;
                    btnUpdate.Enabled = true;
                    BindData(trainID);
                }
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "TrainAdd.aspx", RightChar.Add))
            {
                if (!AccountBiz.CheckRight(account, "TrainList.aspx", RightChar.Add))
                {
                    JSUtility.Alert(this, "您没有增加的权限！");
                    return;
                }
            }
            string empCode = txtEmpCode.Text.Trim();
            if (string.IsNullOrWhiteSpace(empCode))
            {
                JSUtility.Alert(this, "员工编号必须填写");
                return;
            }
            if (!new EmployeeBiz().IsExistEmpCode(empCode))
            {
                JSUtility.Alert(this, "员工编号不存在");
                return;
            }
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("EmpCode", empCode);
            sdList.Add("EmpName", txtEmpName.Text.Trim());
            sdList.Add("DeptName", txtDeptName.Text.Trim());
            sdList.Add("AreaName", txtAreaName.Text.Trim());
            sdList.Add("DutyName", txtDutyName.Text.Trim());
            sdList.Add("JoinTime", txtJoinTime.Text.Trim());
            sdList.Add("TrainItem", txtTrainItem.Text.Trim());
            sdList.Add("TrainTime", txtTrainTime.Text.Trim());
            sdList.Add("Account", account);
            if (!string.IsNullOrWhiteSpace(txtCheckTime.Text.Trim()))
            {
                sdList.Add("CheckTime", txtCheckTime.Text.Trim());
                sdList.Add("CheckGrade", (txtCheckGrade.Text.Trim() == "") ? "0" : txtCheckGrade.Text.Trim());
            }
            sdList.Add("Memo", txtMemo.Text.Trim());
            string info = "";
            if (new MiniBiz().Insert("Admin_Train", sdList))
                info = "增加信息成功";
            else
                info = "增加信息失败";
            JSUtility.Alert(this, info);
        }

        private void BindData(string trainID)
        {
            DataTable dt = trainBiz.GetOneInfo(trainID);
            if (dt.Rows.Count > 0)
            {
                txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                txtDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                txtAreaName.Text = dt.Rows[0]["AreaName"].ToString();
                //
                 string CheckTime=   dt.Rows[0]["CheckTime"].ToString() ;
                 if (Utils.RegexRule.IsDate(CheckTime))
                     txtCheckTime.Text = Convert.ToDateTime(CheckTime).ToString("yyyy-MM-dd");  
                string JoinTime = dt.Rows[0]["JoinTime"].ToString();
                if (Utils.RegexRule.IsDate(JoinTime))
                    txtJoinTime.Text = Convert.ToDateTime(JoinTime).ToString("yyyy-MM-dd");   
                        
               // txtJoinTime.Text = dt.Rows[0]["JoinTime"].ToString() ;
                txtCheckGrade.Text = dt.Rows[0]["CheckGrade"].ToString();
                txtTrainItem.Text = dt.Rows[0]["TrainItem"].ToString();
                string TrainTime = dt.Rows[0]["TrainTime"].ToString();
                if (Utils.RegexRule.IsDate(TrainTime))
                    txtTrainTime.Text = Convert.ToDateTime(TrainTime).ToString("yyyy-MM-dd");   
                        
                //txtTrainTime.Text = dt.Rows[0]["TrainTime"].ToString() ;
                txtMemo.Text = dt.Rows[0]["Memo"].ToString();
            }

        }
        

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "TrainAdd.aspx", RightChar.Mod))
            {
                if (!AccountBiz.CheckRight(account, "TrainList.aspx", RightChar.Mod))
                {
                    JSUtility.Alert(this, "您没有更新的权限！");
                    return;
                }
            }
            if (trainID == "")
            {
                JSUtility.Alert(this, "请先选择数据");
                return;
            }
            string empCode = txtEmpCode.Text.Trim();
            if (string.IsNullOrWhiteSpace(empCode))
            {
                JSUtility.Alert(this, "员工编号必须填写");
                return;
            }
            if (!new EmployeeBiz().IsExistEmpCode(empCode))
            {
                JSUtility.Alert(this, "员工编号不存在");
                return;
            }
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();

            sdList.Add("TrainID", trainID);
            sdList.Add("EmpCode", empCode);
            sdList.Add("EmpName", txtEmpName.Text.Trim());
            sdList.Add("DeptName", txtDeptName.Text.Trim());
            sdList.Add("AreaName", txtAreaName.Text.Trim());
            sdList.Add("DutyName", txtDutyName.Text.Trim());
            sdList.Add("JoinTime", txtJoinTime.Text.Trim());
            sdList.Add("TrainItem", txtTrainItem.Text.Trim());
            sdList.Add("TrainTime", txtTrainTime.Text.Trim());
            sdList.Add("CheckTime", txtCheckTime.Text.Trim());
            sdList.Add("CheckGrade", (txtCheckGrade.Text.Trim() == "") ? "0" : txtCheckGrade.Text.Trim());
            sdList.Add("Memo", txtMemo.Text.Trim());
            sdList.Add("Account", account);
            string info = "";
            if (trainBiz.Update("Admin_Train", sdList))
                info = "更新信息成功";
            else
                info = "更新信息失败";
            JSUtility.Alert(this, info);
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "TrainAdd.aspx", RightChar.Del))
            {
                if (!AccountBiz.CheckRight(account, "TrainList.aspx", RightChar.Del))
                {
                    JSUtility.Alert(this, "您没有删除的权限！");
                    return;
                }
            }
            if (!string.IsNullOrWhiteSpace(trainID))
            {
                if (trainBiz.DeleteOneInfo(trainID))
                    JSUtility.Alert(this, "删除成功");
                else
                    JSUtility.Alert(this, "删除失败");
            }
            else
                JSUtility.Alert(this, "没有选择培训信息");
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            Response.Redirect("TrainAdd.aspx");
        }

        protected void txtEmpCode_TextChanged(object sender, EventArgs e)
        {
            string empCode = txtEmpCode.Text.Trim();
            if (!string.IsNullOrWhiteSpace(empCode))
            {
                if (Utils.StringUtility.IncludeHZ(empCode))//如果包含汉字
                {
                    DataTable dt = new EmployeeBiz().GetOneInfoByName(empCode);
                    if (dt.Rows.Count == 0)
                    {
                        JSUtility.Alert(this, "不存在该员工姓名: " + empCode);
                    }
                    else if (dt.Rows.Count == 1)
                    {
                        if (dt.Rows[0]["StateName"].ToString() != "离职")
                        {
                            txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                            txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                            txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                            txtDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                            txtAreaName.Text = dt.Rows[0]["AreaName"].ToString();
                            string JoinTime = dt.Rows[0]["JoinTime"].ToString();
                            if (Utils.RegexRule.IsDate(JoinTime))
                                txtJoinTime.Text = Convert.ToDateTime(JoinTime).ToString("yyyy-MM-dd");
                        }
                        else
                            JSUtility.Alert(this, "该员工已经离职: " + empCode);
                    }
                    else
                    {
                        string info = "存在同名，请输入工号：\n\n";
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            info += dt.Rows[i]["EmpCode"].ToString() + "  " + dt.Rows[i]["EmpName"].ToString() + "\n";
                        }
                        JSUtility.Alert(this, info);
                    }
                }
                else
                {
                    DataTable dt = new EmployeeBiz().GetOneInfo(empCode);
                    if (dt.Rows.Count > 0)
                    {
                        txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                        txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                        txtDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                        txtAreaName.Text = dt.Rows[0]["AreaName"].ToString();
                        string JoinTime = dt.Rows[0]["JoinTime"].ToString();
                        if (Utils.RegexRule.IsDate(JoinTime))
                            txtJoinTime.Text = Convert.ToDateTime(JoinTime).ToString("yyyy-MM-dd");
                    }
                }
            }
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            JSUtility.CloseWindow();
        }
    }
}